// /账户类型数据
var userTypeData = [
    {id:'普通账户',text:'普通账户'},
    {id:'黑铁会员',text:'黑铁会员'},
    {id:'黄金会员',text:'黄金会员'}
];

var id = CommenMethod.getQueryString("id");
$.ajax({
    type: 'GET',
    url : AjaxPath.rootPath+'/user/'+id,
    contentType:"application/x-www-form-urlencoded",
    success : function(data){
        if(data.status == 200){
            $('#code').val(data.data.code);
            $('#name').val(data.data.name);
            $('#notes').val(data.data.notes);
            $('input[name="status"][value='+data.data.status+']').attr("checked",true);
            var userType = data.data.userType;
            console.log(userType);
            $('.user-type-select2').append('<option selected="selected">'+userType+'</option>');
            $(".user-type-select2").select2({
                data:userTypeData,
                placeholder:'请选择'
            })
        }else{
            alert(data.message);
        }
    },
    error:function(data){
        alert("数据出现错误");
    }
})


//data tables展示数据
$('.update').click(function(){
    $('#user-table').dataTable().fnDestroy();
    var code = $('#code').val();
    var name = $('#name').val();
    var notes = $('#notes').val();
    var status = $('input[name="status"]:checked').val();
    var userType = $('.user-type-select2 option:selected').val();
    if(!paramsCheck(new Array(code,name,notes,status,userType))){
        alert("参数不能为空！");
        return;
    }
    var params = {
        id:id,
        code:code,
        name:name,
        notes:notes,
        status:status,
        userType:userType
    }
    $.ajax({
        type: 'POST',
        url : AjaxPath.rootPath+'/user',
        data:params,
        success : function(data){
            if(data.status == 200){
                alert("修改成功！");
            }else{
                alert(data.message);
            }
        },
        error:function(data){
            alert("修改失败！");
        }
    })
})

var paramsCheck = function(params){
    for(var i = 0; i < params.length; i++){
        if(params[i] == null || params[i] == "" || params[i] == undefined){
            return false;
        }
    }
    return true;
}





